package main;

import java.io.File;
import java.io.FileReader;

import com.fmistuff.threadedcalc.Calculator;
import com.fmistuff.threadedcalc.ui.CalcFrame;
import com.fmistuff.threadedcalc.ui.Log;

public class Main {

	public static void main(String[] args) {
		if (args.length <= 0) {
			new CalcFrame();
		} else {
			String filePath = args[0];
			int numberOfThreads = Integer.parseInt(args[1]);
			String expr = readFromFile(new File(filePath));
			
			Log log = new Log();
			Calculator.calculate(expr, numberOfThreads, log);
			System.out.println(log.getText());
		}
	}

	public static String readFromFile(File file) {
		String expr = null;
		char[] cbuff = new char[(int) file.length()];
		try {
			new FileReader(file).read(cbuff, 0, (int) file.length());
			expr = new String(cbuff);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expr;
	}
}
